package com.ivncorp.evnproject.helpers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.widget.LinearLayout;

public class NetworkChangeReceiver extends BroadcastReceiver {
	
	private LinearLayout status;

	public static boolean isConnected;
	public static String stConnected;
	
	public NetworkChangeReceiver(LinearLayout txt){
		this.status = txt;
	}
	

	@Override
	public void onReceive(final Context context, final Intent intent) {
		if(isNetworkAvailable(context)){
			status.setVisibility(View.GONE);
		}else{
			status.setVisibility(View.VISIBLE);
		}
	}

	private boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						if (!isConnected) {
							if (info[i].getType() == ConnectivityManager.TYPE_WIFI)
								stConnected = "Now you are connected to Internet!+wifi";
							else if (info[i].getType() == ConnectivityManager.TYPE_MOBILE) {
								stConnected = "Now you are connected to Internet!+mobile";
							}
							isConnected = true;
						}
						return true;
					}
				}
			}
		}
		stConnected = "You are not connected to Internet!";
		isConnected = false;
		return false;
	}
}